<%@ page language="java" import="java.util.*" import="java.sql.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加电影名称</title>
</head>
<body>
<%request.setCharacterEncoding("utf-8");%>
<form action="add.jsp" method="post">
<table align="center" border="1">
<caption>添加电影</caption>
<tr>
<th>编号：</th>
<td><input name="id" type="text" required="required"></td>
</tr>
<tr>
<th>电影名称：</th>
<td><input name="name" type="text"></td>
</tr>
<tr>
<th>主演：</th>
<td><input name="author" type="text"></td>
</tr>
<tr>
<th>价格：</th>
<td><input name="price" type="text"></td>
</tr>
<tr>
<td></td>
<th>
	<input type="submit" name="submit" value="添加">
	<input type="reset" value="重置">
</th>
</tr>
</table>
</form>
<%
String submit=request.getParameter("submit");
if(!"".equals(submit)&&submit!=null){
	String id=request.getParameter("id");
	String name=request.getParameter("name");
	String author=request.getParameter("author");
	String price=request.getParameter("price");
	try{
		Class.forName("com.mysql.jdbc.Driver");
		Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gp","root","123456");
		Statement stmt=con.createStatement();
		String sql="insert into gp.books(id,name,author,price)values('"+id+"','"+name+"','"+author+"','"+price+"')";
		int i=stmt.executeUpdate(sql);
		if(i==1){
			out.print("<script>alert('添加成功，单击确定返回主页面！');</script>");
			response.setHeader("refresh", "1;url=index.jsp");
			
		}else{
			out.println("<script>alert('添加失败，单击确定返回');</script>");
			response.setHeader("refresh","1;url=add.jsp");
		}
		con.close();
		stmt.close();
	}catch(Exception e){
		out.println("<script>alert('添加失败,请重试');</script>");
		response.setHeader("refresh","1;url=add.jsp");
	}
}
%>
</body>
</html>